package com.living.player.widget;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.living.R;


/**
 * 动画（暂停、暂停）, 封面 ，显示隐藏等
 */
public class PHAudioView extends RelativeLayout {

    protected RelativeLayout imgAudioAnimation;
    private ObjectAnimator mp3RotateAnimator;
    ImageView img_audio_cover;

    public ImageView getImgAudioAnimation() {
        return img_audio_cover;
    }

    public PHAudioView(Context context) {
        super(context);
        intView(context);
    }

    public PHAudioView(Context context, AttributeSet attrs) {
        super(context, attrs);
        intView(context);
    }

    public PHAudioView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        intView(context);
    }

    @TargetApi(21)
    public PHAudioView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        intView(context);
    }

    public void intView(Context context) {
        LayoutInflater.from(getContext()).inflate(R.layout.ph_player_audio_animation_layout, this);
        imgAudioAnimation = findViewById(R.id.img_audio_animation);
        img_audio_cover = findViewById(R.id.img_audio_cover);
        initRotateAnimation();
    }

    /**
     * 默认初始化动画
     */
    private void initRotateAnimation() {
        /** mp3播放时 动画
         * setInterpolator表示设置旋转速率。 LinearInterpolator为匀速效果，Accelerateinterpolator为加速效果、DecelerateInterpolator为减速效果
         * */
        mp3RotateAnimator = ObjectAnimator.ofFloat(imgAudioAnimation, "rotation", 0F, 360F);
        mp3RotateAnimator.setRepeatCount(ValueAnimator.INFINITE);
        mp3RotateAnimator.setDuration(5000);
        LinearInterpolator lin = new LinearInterpolator();
        mp3RotateAnimator.setInterpolator(lin);
        mp3RotateAnimator.setRepeatCount(-1);
        mp3RotateAnimator.setRepeatMode(ObjectAnimator.RESTART);
    }

    /**
     * 开始动画
     */
    public void resumeMP3Animation() {
        if (getVisibility() == VISIBLE) {
            //如果已经暂停，是继续播放
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                if (mp3RotateAnimator.isStarted()) {
                    mp3RotateAnimator.resume();
                } else {
                    mp3RotateAnimator.start();
                }
            } else {
                if (!mp3RotateAnimator.isRunning()) {
                    mp3RotateAnimator.start();
                }
            }
        }
    }

    /**
     * 暂停结束动画
     */
    public void pauseMP3Animation() {
        if (getVisibility() == VISIBLE) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                if (mp3RotateAnimator.isRunning()) mp3RotateAnimator.pause();
            } else {
                mp3RotateAnimator.end();
            }
        }
    }

    /**
     * 设置封面
     *
     * @param url
     */
    public void setCover(String url) {
//        PlayerLoadUtils.loadAudioImg(getContext(), imgAudioAnimation, url);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }


}
